import { join } from 'path';
import { app, BrowserWindow } from 'electron';
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true';

export const ROOT_PATH = {
  // /dist
  dist: join(__dirname, '../dist'),
  // /dist or /public
  public: join(__dirname, app.isPackaged ? '../dist' : '../public')
};

let win: BrowserWindow | null;
// const preload = join(__dirname, './preload.js')
function createWindow() {
  win = new BrowserWindow({
    width: 800,
    height: 600,
    icon: join(ROOT_PATH.public, 'favicon.ico'), //需要ico图标，否则无法显示图标
    webPreferences: {
      contextIsolation: false,
      nodeIntegration: true
      //   preload,
    }
  });

  if (app.isPackaged) {
    win.loadFile(join(ROOT_PATH.dist, 'index.html'));
  } else {
    win.webContents.openDevTools();
    win.loadURL(process.env.VITE_DEV_SERVER_URL as string);
  }
}

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) createWindow();
});

app.on('ready', async () => {
  createWindow();
});

if (!app.isPackaged) {
  if (process.platform === 'win32') {
    process.on('message', data => {
      if (data === 'graceful-exit') {
        app.quit();
      }
    });
  } else {
    process.on('SIGTERM', () => {
      app.quit();
    });
  }
}

// 修改 Notification 时名称
app.setAppUserModelId('YourAppName');
